﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace YXShop.Web.admin.accessories
{
    public partial class question_edit : System.Web.UI.Page
    {
        protected string LineOption = "none";
        YXShop.BLL.Accessories.Question questionBll = new YXShop.BLL.Accessories.Question();
        protected void Page_Load(object sender, EventArgs e)
        {
            InitWebControl();
            if(!IsPostBack){
                YXShop.Common.PromptInfo.Popedom("012002002","对不起，您没有权限进行编辑");
                YXShop.Common.PromptInfo.Popedom("012002004","对不起，您没有权限进行编辑");
                if (Request["Action"] != null)
                {
                    string action = Request["Action"].ToString();
                    ViewState["Action"] = action;
                    if(Request["id"]!=null){
                        string id = Request["ID"].ToString();
                        ViewState["id"] = id;
                        InitModel(Convert.ToInt32(id));
                    }
                    
                }
               
            }
        }

        //保存
        protected void btnSave_Click(object sender, EventArgs e)
        {
            Save();
        }

        /// <summary>
        /// 验证
        /// </summary>
        private void InitWebControl()
        {
            YXShop.Common.SysParameter sp = new YXShop.Common.SysParameter();
            this.txtAddress.Attributes.Add("readonly", "readonly");
            this.txtAddress.Attributes.Add("onclick", "selectFile('Area',new Array(" + this.hfAddressId.ClientID + "," + this.txtAddress.ClientID + "),310,450,'" + sp.DummyPaht + "');");
            ChangeHope.WebPage.WebControl.Validate(this.txtContent, "请输入问题的内容", "isnull", "必填", "该项为必填项");
            this.Form.Attributes.Add("onsubmit", "return CheckForm()");
        }



        #region 城市
        protected string AreaName(string id)
        {
            string reStr = "";
            YXShop.BLL.SystemInfo.Provinces bll = new YXShop.BLL.SystemInfo.Provinces();
            YXShop.Model.SystemInfo.Provinces model = null;
            string[] Str=id.Split(',');
            for (int i = 0; i < Str.Length; i++)
            {
                if (Str[i] != "")
                {
                    model = bll.GetModel(Convert.ToInt32(Str[i]));
                    if (model != null)
                    {
                        if (reStr == "")
                        {
                            reStr = model.CityName;
                        }
                        else
                        {
                            reStr =reStr+","+ model.CityName;
                        }
                    }
                }
            }
            return reStr;
        }
        #endregion

        #region 初始化
        protected void InitModel(int id)
        {
            YXShop.Model.Accessories.Question model = questionBll.GetModelById(id);
            this.txtContent.Text = model.QuestionContent;
            this.txtAddress.Text = model.QuestionArea != string.Empty ? AreaName(model.QuestionArea) : string.Empty;
            this.hfAddressId.Value = model.QuestionArea;
            this.rbPutout.SelectedValue = model.Necessarily.ToString();
            this.rbSingleText.Checked = model.QuestionType.ToString() == "1" ? true : false;
            this.rbMunitText.Checked = model.QuestionType.ToString() == "2" ? true : false;
            this.rbSingleOption.Checked = model.QuestionType.ToString() == "3" ? true : false;
            this.rbMumintOption.Checked = model.QuestionType.ToString() == "4" ? true : false;
            this.rbSelect.Checked = model.QuestionType.ToString() == "5" ? true : false;
            this.rbPutoutOption.Checked = model.QuestionType.ToString() == "6" ? true : false;

            if (model.QuestionType.ToString() == "3" || model.QuestionType.ToString() == "4" || model.QuestionType.ToString() == "5")
            {

                YXShop.BLL.Accessories.Question daca = new YXShop.BLL.Accessories.Question();
                string strContent = daca.GetModelById(id).QuestionDiscription;

                string[] arrContent = strContent.Split('|');

                string str = "";
                foreach (string strContentIN in arrContent)
                {
                    string str1 = strContentIN.Split(',')[0].ToString();
                    if (str == "")
                    {
                        str = str1;
                    }
                    else
                    {
                        str = str + "|" + str1;
                    }
                }
                txtOption.Text = str;

            }

        }
        #endregion

        #region 保存/修改
        /// <summary>
        /// 保存
        /// </summary>
        protected void Save()
        {   
            string questID=Request["QuestID"].ToString();
            ViewState["questId"] = questID;
            YXShop.Model.Accessories.Question model = new YXShop.Model.Accessories.Question();
            model.QuestionId = Convert.ToInt32(questID);
            model.QuestionContent = this.txtContent.Text.Trim().ToString();
            string QuestionType = "0";
            if (this.rbSingleText.Checked == true)
                QuestionType = "1";
            if (this.rbMunitText.Checked == true)
                QuestionType = "2";
            if (this.rbSingleOption.Checked == true)
                QuestionType = "3";
            if (this.rbMumintOption.Checked == true)
                QuestionType = "4";
            if (this.rbSelect.Checked == true)
                QuestionType = "5";
            if (this.rbPutoutOption.Checked == true)
                QuestionType = "6";
            model.QuestionType = Convert.ToInt32(QuestionType);
            if (QuestionType == "3" || QuestionType == "4" || QuestionType == "5")
            {
                if (txtOption.Text != string.Empty)
                {
                    string strwt = txtOption.Text.Trim();
                    string[] arrContent = strwt.Split('|');
                    string str = "";
                    foreach (string strContentIN in arrContent)
                    {
                        string str1 = " " + strContentIN.Split(',')[0].ToString() + ",0";
                        if (str == "")
                        {
                            str = str1;
                        }
                        else
                        {
                            str = str + "|" + str1;
                        }

                    }
                    model.QuestionDiscription = str;
                }
            }
            if (QuestionType == "6")
            {
                model.QuestionDiscription = "是,0|否,0";
            }
            string resultStr = "0";
            model.Necessarily = Convert.ToInt32(this.rbPutout.SelectedValue);
            model.InvestigateStateDate = System.DateTime.Now;
            model.InvestigateEndDate = System.DateTime.Now;
            model.Result = resultStr;
            model.QuestionArea = this.hfAddressId.Value != string.Empty ? this.hfAddressId.Value.ToString() : "";
            if(ViewState["Action"].ToString() == "Add"){
                questionBll.Add(model);
                this.ltlMsg.Text = "操作成功，已保存该信息";
                this.pnlMsg.Visible = true;
                this.pnlMsg.CssClass = "actionOk";
            }
            else if (ViewState["Action"].ToString()=="edit")
            {
                model.Id = Convert.ToInt32(ViewState["id"]);
                questionBll.Update(model);
                this.ltlMsg.Text = "操作成功，已修改该信息";
                this.pnlMsg.Visible = true;
                this.pnlMsg.CssClass = "actionOk";
            }
        }
        #endregion
    }
}
